//waterline引入
var Waterline = require('waterline');
var mongoAdapter = require('sails-mongo'); //waterline的适配器模块
var acl = require('acl'); //node-acl管理系统模块

//引入mongodb相关
const mongodb = require('./mongodb');

//解析yaml配置redis文件
const yaml = require('js-yaml');
const fs = require('fs');
try {
    var doc = yaml.safeLoad(fs.readFileSync('/obj/monitor-server/config/center.conf', 'utf8'));
    console.log(doc);
} catch (e) {
    console.log(e);
}

//连接地址
var connections = {
    localMongo: {
        adapter: mongodb.adapter,
        host: mongodb.host,
        port: mongodb.port,
        database: mongodb.database,
        user: mongodb.user,
        password: mongodb.password,
        authMechanism: mongodb.authMechanism,
        authSource: mongodb.authSource
    }
};

//适配器
var adapters = {
    mongo: mongoAdapter,
    default: 'mongo'
}

//waterline配置
var waterlineConfig = {
    adapters: adapters,
    connections: connections
}

//新建waterline
var orm = new Waterline();

//注册model
orm.loadCollection(require('../models/acl_user'));
orm.loadCollection(require('../models/acl_role'));
orm.loadCollection(require('../models/acl_resource'));
orm.loadCollection(require('../models/prov_city_area')); //注册省市区model
orm.loadCollection(require('../models/substation_manage')); //注册站点管理
orm.loadCollection(require('../models/deviceManage')); //设备管理

//初始化acl
var MongoClient = require('mongodb').MongoClient;
var db_of_acl = waterlineConfig.connections.localMongo;
var url = 'mongodb://' + db_of_acl.user + ':' + db_of_acl.password + '@' + db_of_acl.host + ':' + db_of_acl.port + '/' + db_of_acl.database + '?authMechanism=' + db_of_acl.authMechanism + '&authSource=' + db_of_acl.authSource;

MongoClient.connect(url, function(err, db) {
    // console.log(db);
    if (err) throw err;
    global.acl = new acl(new acl.mongodbBackend(db, 'acl_'));
});
exports.config = waterlineConfig;
exports.orm = orm;